#@layout()

#define content()

<div class="easyui-layout" data-options="fit:true">
    <div data-options="region:'center',border:false" style="padding: 2px;">
        <div class="easyui-panel" title="策略信息" style="width:100%;padding:10px;"
             data-options="iconCls:'icon-application_form'">
            #(_res.get('policy.form.manager.policy')):

            <input id="policyName" class="easyui-combobox" style="width: 250px;" data-options="
                    valueField: 'pl_id',
                    textField: 'policy_name',
                    editable:false,
                    url: '#(ctx)/policy/getPolicyListByTenantId'">
            &nbsp;

            #for(resource : resourceList)
                <a href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'#(resource.icon)',plain:true" id='#(resource.url)'>#(_res.get(resource.resourceName))</a>
            #end
        </div>


        <div class="easyui-layout" data-options="fit:true" style="margin-top: 5px;">
            <div data-options="region:'west',split:true,iconCls:'icon-application_view_columns',border:false"
                 style="width:50%;">
                <div id="authMetadataDatagrid"></div>
            </div>
            <div data-options="region:'center',split:false,iconCls:'icon-application_view_columns',border:false"
                 style="width:50%;">
                <div id="acceptPolicyDatagrid"></div>
            </div>
        </div>


        <div id="authMetadataTb" style="padding: 5px;">
            <div class="row">
                <div class="col-md-12">
                    <a href="javascript:;" class="easyui-linkbutton" data-options="plain:true"
                       iconCls="icon-shape_move_forwards" id="addAuthMetadataToPolicy">添加元数据到策略</a>
                </div>
            </div>
        </div>

        <div id="acceptPolicyTb" style="padding: 5px;">
            <div class="row">
                <div class="col-md-12">
                    <a href="javascript:;" id="removeAuthMetadataInAccept" data-options="plain:true"
                       iconCls="icon-shape_move_forwards" class="easyui-linkbutton">移除元数据</a>
                </div>
            </div>
        </div>
    </div>





</div>

#end


#define css()

#end


#define js()

<script type="text/javascript">

    var authMetadataDatagrid = 'authMetadataDatagrid', acceptPolicyDatagrid = 'acceptPolicyDatagrid',
        authMetadataTb = 'authMetadataTb', acceptPolicyTb = 'acceptPolicyTb';
    var policyName = 'policyName';

    $(function () {

        //初始化表格
        $("#" + authMetadataDatagrid).datagrid({
            url: contextRootPath + '/authMetadata/getNonAuthMetadataByPolicyId',
            rownumbers: true,
            title: '元数据',
            iconCls: 'icon-application_form',
            toolbar: '#' + authMetadataTb,
            border: true,
            singleSelect: false,
            nowrap: false,
            striped: true,
            fit: true,
            fitColumns: true,
            columns: [[
                {
                    field: DBConts.TABLE_AUTH_METADATA.METADATA_ID,
                    title: DBConts.TABLE_AUTH_METADATA.METADATA_ID,
                    checkbox: true
                },
                {
                    field: DBConts.TABLE_AUTH_METADATA.AAID,
                    title: 'AAID',
                    sortable: true
                },
                {
                    field: DBConts.TABLE_AUTH_METADATA.DESCRIPTION,
                    title: "#(_res.get('auth.form.description'))",
                    sortable: true
                }
            ]]
        });

        //初始化表格
        $("#" + acceptPolicyDatagrid).datagrid({
            url: contextRootPath + '/authMetadata/getAuthMetadataByPolicyId',
            rownumbers: true,
            title: '待选元数据',
            iconCls: 'icon-application_form',
            border: true,
            toolbar: '#' + acceptPolicyTb,
            singleSelect: false,
            nowrap: false,
            striped: true,
            fit: true,
            fitColumns: true,
            columns: [[
                {
                    field: DBConts.TABLE_AUTH_METADATA.METADATA_ID,
                    title: DBConts.TABLE_AUTH_METADATA.METADATA_ID,
                    checkbox: true
                },
                {
                    field: DBConts.TABLE_AUTH_METADATA.AAID,
                    title: 'AAID',
                    sortable: true
                },
                {
                    field: DBConts.TABLE_AUTH_METADATA.DESCRIPTION,
                    title: "#(_res.get('auth.form.description'))",
                    sortable: true
                }
            ]]
        });


    #for(resource : resourceList)

        #if(resource.url == 'policy_add')
            $("##(resource.url)").on('click', function () {
                var params = {};
                params.title = "#(_res.get('policy.form.add'))";
                params.width = 700;
                params.height = 250;
                params.top = '10px';
                params.modal = true;
                params.iconCls = 'icon-application_edit';
                params.url = contextRootPath + '/policy/add';
                params.buttons = [{
                    text: '提交',
                    iconCls: 'icon-application_go',
                    handler: function () {
                        datagrid_save('add');
                    }
                }, {
                    text: '关闭',
                    iconCls: 'icon-application_side_contract',
                    handler: function () {
                        $('#crud_dlg').dialog('close');
                    }
                }];

                crud_dlg(params);
            });

        #elseif(resource.url == 'policy_remove')
            $("##(resource.url)").on('click', function () {
                var policyId = $('#policyName').combobox('getValue');

                if (policyId == '') {
                    $.messager.alert("提示框", "请选择需要操作的策略!", "warning");
                    return;
                }

                $.messager.confirm('确认框', '请确定需要删除所选择的策略吗？', function (r) {
                    if (r) {
                        $.post(contextRootPath + "/policy/remove?policy.pl_id=" + policyId, function (data) {
                            var params = {};
                            params.element = policyName;
                            params.elementType = WIDGET_TYPE.COMBOBOX;
                            params.data = data;
                            params.isClose = false;
                            //resultCallback(params);

                            $.messager.alert('提示', params.data.message, 'info', function () {
                                location.reload();
                            });
                        }, 'json');
                    }
                });
            });

        #elseif(resource.url == 'policy_config_manager')
            $("##(resource.url)").on('click', function () {
                var policyId = $('#policyName').combobox('getValue');

                if (policyId == '') {
                    $.messager.alert("提示框", "请选择需要操作的策略!", "warning");
                    return;
                }

                var params = {};
                params.title = "#(_res.get(resource.resourceName))";
                params.width = 2000;
                params.height = 2000;
                params.top = '10px';
                params.modal = true;
                params.iconCls = 'icon-shape_ungroup';
                params.url = contextRootPath + '/policy/policyConfigManager?policy.pl_id='+ policyId;

                crud_dlg(params);
            });
        #end
    #end


        $('#policyName').combobox({
            onChange: function (newValue, oldValue) {
                $("#" + authMetadataDatagrid).datagrid("reload", {
                    'policy.pl_id': newValue
                });
                $("#" + acceptPolicyDatagrid).datagrid("reload", {
                    'policy.pl_id': newValue
                });
            }
        });


        $("#addAuthMetadataToPolicy").on('click', function () {
            var policyName = $('#policyName').combobox("getValue");
            if (policyName == '') {
                $.messager.alert("提示框", "请选择您需要操作的策略!", "warning");
                return;
            }

            var selections = $('#' + authMetadataDatagrid).datagrid('getSelections');
            var ids = "";

            if (selections.length < 1) {
                $.messager.alert("提示框", "请选择需要操作的元数据!", "warning");
                return;
            }

            $.each(selections, function (i, v) {
                ids += v[DBConts.TABLE_AUTH_METADATA.METADATA_ID] + ",";
            });

            var params = {
                "plid": $('#policyName').combobox('getValue'),
                "ids": ids,
                "pl_name": $('#policyName').combobox('getText')
            };

            $.post(contextRootPath + "/policy/savePolicyAuth", params, function (data) {
                $.messager.alert("提示", data.message, 'info', function () {
                    $("#" + authMetadataDatagrid).datagrid("reload", {
                        'policy.pl_id': $('#policyName').combobox('getValue')
                    });
                    $("#" + acceptPolicyDatagrid).datagrid("reload", {
                        'policy.pl_id': $('#policyName').combobox('getValue')
                    });
                });
            }, 'json');
        });


        $("#removeAuthMetadataInAccept").on('click', function () {
            var policyName = $('#policyName').combobox("getValue");
            if (policyName == '') {
                $.messager.alert("提示框", "请选择您需要操作的策略!", "warning");
                return;
            }

            var selections = $('#' + acceptPolicyDatagrid).datagrid('getSelections');
            var ids = "";

            if (selections.length < 1) {
                $.messager.alert("提示框", "请选择需要操作的元数据!", "warning");
                return;
            }

            $.each(selections, function (i, v) {
                ids += v[DBConts.TABLE_AUTH_METADATA.METADATA_ID] + ",";
            });

            var params = {
                "plid": $('#policyName').combobox('getValue'),
                "ids": ids,
                "pl_name": $('#policyName').combobox('getText')
            };

            $.post(contextRootPath + "/policy/removeByPolicyIdAndMetadataId", params, function (data) {
                $.messager.alert("提示", data.message, 'info', function () {
                    $("#" + authMetadataDatagrid).datagrid("reload", {
                        'policy.pl_id': $('#policyName').combobox('getValue')
                    });
                    $("#" + acceptPolicyDatagrid).datagrid("reload", {
                        'policy.pl_id': $('#policyName').combobox('getValue')
                    });
                });
            }, 'json');
        });

    });
</script>

#end



